<?php

namespace aliservice\think\apisign;

use think\Request;
use Closure;

class ApiSignCheckMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $config = config('api_sign') ?? false;
        if ($config['debug']) {
            return $next($request);
        }
        $exclude_url_list = $config['exclude_url_list'] ?? [];
        if (in_array($request->url(), $exclude_url_list)) {
            return $next($request);
        }
        $data = $request->isPost() ? $request->post() : $request->get();
        $timestamp = $request->header('timestamp', '');
        $randStr   = $request->header('randStr', '');
        $sign      = $request->header('sign', '');
        app('api_sign')->check($data, $timestamp, $randStr, $sign);
        return $next($request);
    }
}